package com.felight.ems.strings;
import java.io.*;
public class StringDemo{
	public static boolean writeEmails(File file,String data){
		FileWriter fileWriter = null;
		BufferedWriter bufferedWriter = null;
		try{
			if(file.canWrite()){
				FileWriter fileWriter = new FileWriter(file);
				BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
				bufferedWriter.write(data);
				bufferedWriter.newLine();
				bufferedWriter.close();
			}else{
				file.createNewFile();
				bufferedWriter.write(data);
				bufferedWriter.newLine();
				bufferedWriter.close();
			}
			return true;
		}
		catch(IOException ex){
			System.out.println("Error while writing to file'"+file+"'"+ex.toString());
			return false;
		}
		finally{
			try{
				bufferedWriter.flush();
				bufferedWriter.close();
				fileWriter.flush();
				fileWriter.close();
			}
			catch(Exception ex){
				bufferedWriter = null;
				fileWriter = null;
				ex.printStackTrace();
			}
		}
	}
	public void readText(){
		String fileName = "accounts.txt";
		String line = null;
		try{
			FileReader fileReader = new FileReader(fileName);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			while((line=bufferedReader.readLine())!=null){
				System.out.println(line);
			}
			bufferedReader.close();
		}
		catch(FileNotFoundException ex){
			System.out.println("Unable to open file'"+fileName+"'");
		}
		catch(IOException ex){
			System.out.println("Error while reading file '"+fileName+"'");
		}
	}
	public static void main(String[] args){
		
	}
}